package com.Modular.pdf_Util.examplesPDF.CheLiangAnQuanSheShiJianCha;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import com.Modular.MinioPlugin.MinIoOperations;
import com.Modular.pdf_Util.PDF_utils;
import com._Config.Model.errorLogModel;
import com.jfinal.core.Controller;
import com.jfinal.json.Json;
import com.jfinal.log.Log4jLog;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 车辆调度日志登记
 */
public class CheLiangAnQuanSheShiJianChaService extends Controller {
    static Log4jLog logger = Log4jLog.getLog(CheLiangAnQuanSheShiJianChaService.class);

    public static final CheLiangAnQuanSheShiJianChaService service = new CheLiangAnQuanSheShiJianChaService();

    public String buildPdf(String danWeiDaiMa,String startTime,String endTime) {

        try {
            if(startTime==null&&endTime==null) {
                startTime = DateUtil.format(DateUtil.lastWeek(), "yyyy-MM-dd") + " 00:00:00";
                endTime = DateUtil.format(new Date(), "yyyy-MM-dd")+" 23:59:59";
            }
            String jainChaLeiXing="车辆安全设施检查";
            List<errorLogModel> dataList
                    = errorLogModel.dao.find("SELECT * FROM T_AnQuanShengChanRiChangGuanLi_CheLiangAnQuanJianCha " +
                            " WHERE sys_xitongzhuangtai=0 AND SYS_DanWeiDaiMa=? and JianChaRiQi >=? AND JianChaRiQi <=? and JainChaLeiXing=? "
                    , danWeiDaiMa, startTime, endTime,jainChaLeiXing);

            List<Map<String, Object>> mapList = new ArrayList<>();
            String qiYeMingCheng = null;
            for (int i = 0; i < dataList.size(); i++) {
                mapList.add(diaoDuRiZhiToMap(dataList.get(i), i + 1));
                qiYeMingCheng = (String) mapList.get(0).get("QiYeMingCheng");
            }
            String time = DateFormatUtils.format(new Date(), "yyyy-MM-dd");
            Map<String, String> titleMap = new HashMap<>();
            titleMap.put("QiYeMingCheng", qiYeMingCheng);
            titleMap.put("Time", time);
            titleMap.put("StartTime",startTime);
            titleMap.put("EndTime",endTime);

            String dir = System.getProperty("user.dir");
            String fileName = String.format("%s%s.pdf", "车辆安全设施检查表", DateUtil.format(new Date(), "yyyyMMddHHmmss"));
            String outPath = String.format("%s%s%s", dir, "/webapp/WEB-INF/template/temp/", fileName);

            String pdfName = String.format("%s.pdf", "车辆安全设施检查表-" + time);
            //生成本地pdf 一表单+一表格
            PDF_utils.buildPdf("车辆安全设施检查表-模板.docx", outPath, titleMap, 2, mapList);
            logger.debug("fileName:%s,output:%s", fileName, outPath);
            MinIoOperations.fileUpload("asiatrip", pdfName, outPath);
            String geturl = MinIoOperations.fileGeturlGet("asiatrip", pdfName);

            String SYS_DanWeiMingCheng=null;
            if(dataList.size()>0){
                SYS_DanWeiMingCheng=dataList.get(0).getStr("SYS_DanWeiMingCheng");
            }
            //关连电子档案
            dangAnWenJian("车辆安全设施检查",pdfName,geturl,null,SYS_DanWeiMingCheng,danWeiDaiMa);
            return geturl;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public Map<String, Object> diaoDuRiZhiToMap(errorLogModel model, int num) {
        Map<String, Object> data = new HashMap<>();
        data.put("QiYeMingCheng", model.getStr("QiYeMingCheng"));
        data.put("ChePaiHaoMa", model.getStr("ChePaiHaoMa"));
        data.put("ChePaiYanSe", model.getStr("ChePaiYanSe")==null?"":model.getStr("ChePaiYanSe"));
        data.put("JianChaRiQi", DateUtil.parse(model.getStr("JianChaRiQi")).toString("yyyy-MM-dd HH:mm"));
        data.put("JianChaRenYuan", model.getStr("JianChaRenYuan")==null?"":model.getStr("JianChaRenYuan"));
        data.put("num", num);
        data.put("BeiZhu",model.getStr("BeiZhu")==null?"":model.getStr("BeiZhu"));
        data.put("ZuoWeiShu", model.getStr("ZuoWeiShu")==null?"":model.getStr("ZuoWeiShu"));
        data.put("AnQuanDai", "");
        data.put("YingJiChui", "");
        data.put("MieHuoQi", "");
        data.put("SanJiaoJingShiPai", "");
        data.put("SanJiaoMu", "");
        data.put("YingJiMen", "");
        data.put("YingJiChuang", "");
        data.put("AnQuanDingChuang", "");
        data.put("BoFangQi","");
        String jianChaJson = model.getStr("JianChaNeiRong");
        if (jianChaJson != null) {
            JSONArray json = JSONUtil.parseArray(jianChaJson);
            for (int i = 0; i < json.size(); i++) {
                JSONObject m= json.getJSONObject(i);
//                String key= (String) m.keySet().toArray()[0];
                String value= m.getStr("CanShuMingCheng");
                String jieGuo= m.getStr("JieGuo");
                jieGuo=jieGuo==null?"":jieGuo;
                if ("安全带".equals(value)) {
                    data.put("AnQuanDai",jieGuo);
                }
                if ("应急锤".equals(value)) {
                    data.put("YingJiChui",jieGuo);
                }
                if ("灭火器".equals(value)) {
                    data.put("MieHuoQi",jieGuo);
                }
                if ("三角警示牌".equals(value)) {
                    data.put("SanJiaoJingShiPai",jieGuo);
                }
                if ("三角木".equals(value)) {
                    data.put("SanJiaoMu",jieGuo);
                }
                if ("应急门".equals(value)) {
                    data.put("YingJiMen",jieGuo);
                }
                if ("应急窗".equals(value)) {
                    data.put("YingJiChuang",jieGuo);
                }
                if ("安全顶窗".equals(value)) {
                    data.put("AnQuanDingChuang",jieGuo);
                }
                if ("播放器、视频".equals(value)) {
                    data.put("BoFangQi",jieGuo);
                }
            }
        }
        return data;
    }

    //关连电子档案
    public void dangAnWenJian(String muLuName,String fileName,String filePath,String SYS_DanWeiDianNaoBianHao ,
                              String SYS_DanWeiMingCheng ,String SYS_DanWeiDaiMa){
        //创建文件记录（电子档案）
    	errorLogModel fileModel=new errorLogModel();
        fileModel.set("Id",  IdUtil.randomUUID());
        fileModel.set("ZhuId","13b8805f-2dd9-46dd-ac10-e4d409ac1116");//车辆安全检查
        fileModel.set("WenJianZiLei",muLuName);
        fileModel.set("WenJianLeiBie","隐患排查与治理");
        fileModel.set("WenJianMingCheng",fileName);
        fileModel.set("WenJianLaiYuan","系统生成");
        fileModel.set("WenJianWeiZhi",filePath);
        fileModel.set("SYS_DanWeiDianNaoBianHao",SYS_DanWeiDianNaoBianHao);
        fileModel.set("SYS_DanWeiMingCheng",SYS_DanWeiMingCheng);
        fileModel.set("SYS_DanWeiDaiMa",SYS_DanWeiDaiMa);
        fileModel.set("GengXinShiJian",new Date());
        fileModel.set("BianHao",1);
        fileModel.save();
    }
}
